package com.imay.live.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.base.util.StringUtil;
import com.imay.base.widget.BackgroundDrawableSpan;
import com.imay.base.widget.VerticalImageSpan;
import com.imay.live.R;
import com.imay.live.helper.GiftHelper;
import com.imay.live.helper.LevelHelper;
import com.imay.live.proto.room.RoomActivityTipsNotify;
import com.imay.live.proto.room.RoomChatNotify;
import com.imay.live.proto.room.RoomGameHostAgreeInviteNotify;
import com.imay.live.proto.room.RoomGameHostRefuseInviteNotify;
import com.imay.live.proto.room.RoomGameInviteNotify;
import com.imay.live.proto.room.RoomHouseNotify;
import com.imay.live.proto.room.RoomKickUserNotify;
import com.imay.live.proto.room.RoomLikeNotify;
import com.imay.live.proto.room.RoomRankUpgradeNotify;
import com.imay.live.proto.room.RoomSendGiftNotify;
import com.imay.live.proto.room.RoomSilenceUserNotify;
import com.imay.live.proto.room.RoomTips;
import com.imay.live.proto.room.RoomUserEnterNotify;
import com.imay.live.proto.user.UserInfoCard;
import com.imay.base.util.SpannableStringUtils;

import java.util.List;

/**
 * Created by jeaman on 2017/4/2.
 * 房间聊天消息
 */

public class RoomChatAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {

    public RoomChatAdapter(List<Object> data) {
        super(R.layout.item_room_chat, data);
    }

    public UserInfoCard getUser(int position) {
        UserInfoCard userInfoCard = null;
        Object item = getItem(position);
        if (item instanceof RoomChatNotify) {
            userInfoCard = ((RoomChatNotify) item).sender;
        } else if (item instanceof RoomUserEnterNotify) {
            userInfoCard = ((RoomUserEnterNotify) item).user;
        } else if (item instanceof RoomLikeNotify) {
            userInfoCard = ((RoomLikeNotify) item).user;
        } else if (item instanceof RoomSendGiftNotify) {
            userInfoCard = ((RoomSendGiftNotify) item).sender;
        } else if (item instanceof RoomGameInviteNotify) {
            userInfoCard = ((RoomGameInviteNotify) item).fromuser;
//        } else if (item instanceof RoomGameHostAgreeInviteNotify) {
//            userInfoCard = ((RoomGameHostAgreeInviteNotify) item).fromuser;
//        } else if (item instanceof RoomGameHostRefuseInviteNotify) {
//            userInfoCard = ((RoomGameHostRefuseInviteNotify) item).fromuser;
        } else if (item instanceof RoomKickUserNotify) {
            userInfoCard = ((RoomKickUserNotify) item).actionUser;
        } else if (item instanceof RoomSilenceUserNotify) {
            userInfoCard = ((RoomSilenceUserNotify) item).actionUser;
        }else if(item instanceof RoomHouseNotify){
            userInfoCard = ((RoomHouseNotify) item).sender;
        } else if (item instanceof RoomRankUpgradeNotify) {
            userInfoCard = ((RoomRankUpgradeNotify) item).userData;
        }
        return userInfoCard;
    }

    //获取活动直播公告
    public RoomActivityTipsNotify getNotify(int position) {
        Object item = getItem(position);
        if (item instanceof RoomActivityTipsNotify) {
            return (RoomActivityTipsNotify) item;
        }
        return null;
    }

    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        TextView messageTextView = helper.getView(R.id.item_room_chat_tv_message);
        Button jumpBtn = helper.getView(R.id.item_room_chat_tv_jump);
        helper.addOnClickListener(R.id.item_room_chat_tv_message);
        helper.addOnClickListener(R.id.item_room_chat_tv_jump);

        UserInfoCard userInfoCard = null;
        String space = mContext.getString(R.string.string_space);
        String colon = mContext.getString(R.string.string_colon);
        SpannableStringBuilder levelSpan = null;
        SpannableStringBuilder spannableStringBuilder = null;
         if (item instanceof RoomActivityTipsNotify) {    //活动直播公告
             RoomActivityTipsNotify roomActivityTipsNotify = (RoomActivityTipsNotify) item;
             spannableStringBuilder = SpannableStringUtils.getBuilder(roomActivityTipsNotify.title + roomActivityTipsNotify.content).setForegroundColor(getColor(R.color.text_chat_notify))
                    .create();
             if (!TextUtils.isEmpty(roomActivityTipsNotify.jumpContentTitle)) {
                 jumpBtn.setText(roomActivityTipsNotify.jumpContentTitle);
                 jumpBtn.setVisibility(View.VISIBLE);
             }
        } else {
             jumpBtn.setVisibility(View.GONE);
             if (item instanceof RoomTips) { //公告
                 RoomTips roomTips = (RoomTips) item;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(roomTips.getContentTitle()+ colon + roomTips.getContent()).setForegroundColor(getColor(R.color.text_chat_notify))
                         .create();
             } else if (item instanceof RoomChatNotify) {   //聊天消息
                 RoomChatNotify roomChatNotify = (RoomChatNotify) item;
                 userInfoCard = roomChatNotify.sender;
                 SpannableStringUtils.Builder builder;
                 if (roomChatNotify.at != null) {
                     builder = SpannableStringUtils.getBuilder(mContext.getString(R.string.live_room_chat_at, roomChatNotify.at.nick)).setForegroundColor(getColor(R.color.text_chat_at));
                 } else {
                     builder = SpannableStringUtils.getBuilder(colon);
                 }
                 String content = roomChatNotify.content != null ? roomChatNotify.content : "";
                 spannableStringBuilder = builder.append(content).create();
             } else if (item instanceof RoomUserEnterNotify) {   //进入房间
                 RoomUserEnterNotify roomUserEnterNotify = (RoomUserEnterNotify) item;
                 userInfoCard = roomUserEnterNotify.user;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(space)
                         .append(mContext.getString(R.string.live_room_chat_entry))
                         .create();
             } else if (item instanceof RoomLikeNotify) {    //点赞、关注、分享
                 RoomLikeNotify roomLikeNotify = (RoomLikeNotify) item;
                 userInfoCard = roomLikeNotify.user;
                 String message = null;
                 switch (roomLikeNotify.action) {
                     case 0:
                     case 1://点赞
                         message = mContext.getString(R.string.live_room_chat_like);
                         break;
                     case 2://关注
                         message = mContext.getString(R.string.live_room_chat_follow);
                         break;
                     case 3://分享
                         message = mContext.getString(R.string.live_room_chat_share);
                         break;
                 }
                 if (message != null) {
                     spannableStringBuilder = SpannableStringUtils.getBuilder(space)
                             .append(message).setForegroundColor(getColor(R.color.text_chat_share))
                             .create();
                 }
             } else if (item instanceof RoomSendGiftNotify) {    //礼物
                 RoomSendGiftNotify roomSendGiftNotify = (RoomSendGiftNotify) item;
                 userInfoCard = roomSendGiftNotify.sender;
                 String message = mContext.getString(R.string.live_room_chat_gift, roomSendGiftNotify.count, GiftHelper.getInstance().getGiftName(roomSendGiftNotify.giftId));
                 if (roomSendGiftNotify.Rate > 0) {
                     message += space + mContext.getString(R.string.live_room_chat_gift_lucky, roomSendGiftNotify.Rate);
                 }
                 spannableStringBuilder = SpannableStringUtils.getBuilder(space)
                         .append(message).setForegroundColor(getColor(R.color.text_chat_gift))
                         .create();
             } else if (item instanceof RoomKickUserNotify) {    //踢人
                 RoomKickUserNotify roomKickUserNotify = (RoomKickUserNotify) item;
                 userInfoCard = roomKickUserNotify.actionUser;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(space)
                         .append(mContext.getString(R.string.live_room_chat_kick_1))
                         .append(space)
                         .append(roomKickUserNotify.kickedUser.nick).setForegroundColor(getColor(R.color.text_chat_at))
                         .append(space)
                         .append(mContext.getString(R.string.live_room_chat_kick_2))
                         .create();
             } else if (item instanceof RoomSilenceUserNotify) {    //禁言
                 RoomSilenceUserNotify roomSilenceUserNotify = (RoomSilenceUserNotify) item;
                 userInfoCard = roomSilenceUserNotify.actionUser;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(space)
                         .append(mContext.getString(R.string.live_room_chat_silence_1))
                         .append(space)
                         .append(roomSilenceUserNotify.kickedUser.nick).setForegroundColor(getColor(R.color.text_chat_at))
                         .append(space)
                         .append(mContext.getString(R.string.live_room_chat_silence_2))
                         .create();
             } else if (item instanceof RoomGameInviteNotify) {  //游戏邀请
                 RoomGameInviteNotify gameNotify = (RoomGameInviteNotify) item;
                 userInfoCard = gameNotify.fromuser;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(space)
                         .append(mContext.getString(R.string.live_room_chat_game_invite_1)).setForegroundColor(getColor(R.color.text_chat_share))
                         .append(space)
                         .append(gameNotify.GameName).setForegroundColor(getColor(R.color.text_chat_game))
                         .append(mContext.getString(R.string.live_room_chat_game_invite_2)).setForegroundColor(getColor(R.color.text_chat_share))
                         .create();
             } else if (item instanceof RoomGameHostAgreeInviteNotify) { //同意游戏邀请
                 RoomGameHostAgreeInviteNotify gameNotify = (RoomGameHostAgreeInviteNotify) item;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(mContext.getString(R.string.live_room_chat_game_agree_1)).setForegroundColor(getColor(R.color.text_chat_share))
                         .append(space)
                         .append(gameNotify.fromuser.nick).setForegroundColor(getColor(R.color.text_chat_at))
                         .append(space)
                         .append(mContext.getString(R.string.live_room_chat_game_agree_2)).setForegroundColor(getColor(R.color.text_chat_share))
                         .append(space)
                         .append(gameNotify.GameName).setForegroundColor(getColor(R.color.text_chat_game))
                         .append(space)
                         .append(mContext.getString(R.string.live_room_chat_game_agree_3)).setForegroundColor(getColor(R.color.text_chat_share))
                         .create();
             } else if (item instanceof RoomGameHostRefuseInviteNotify) {    //拒绝游戏邀请
                 RoomGameHostRefuseInviteNotify gameNotify = (RoomGameHostRefuseInviteNotify) item;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(mContext.getString(R.string.live_room_chat_game_refuse_1)).setForegroundColor(getColor(R.color.text_chat_share))
                         .append(space)
                         .append(gameNotify.fromuser.nick).setForegroundColor(getColor(R.color.text_chat_at))
                         .append(space)
                         .append(mContext.getString(R.string.live_room_chat_game_refuse_2)).setForegroundColor(getColor(R.color.text_chat_share))
                         .append(space)
                         .append(gameNotify.GameName).setForegroundColor(getColor(R.color.text_chat_game))
                         .append(space)
                         .append(mContext.getString(R.string.live_room_chat_game_refuse_3)).setForegroundColor(getColor(R.color.text_chat_share))
                         .create();
             } else if (item instanceof RoomRankUpgradeNotify) {
                 RoomRankUpgradeNotify roomRankUpgradeNotify = (RoomRankUpgradeNotify) item;
                 if (roomRankUpgradeNotify.rankType == 1) {   //主播日榜
                     if (roomRankUpgradeNotify.rank == 1) {
                         spannableStringBuilder = SpannableStringUtils.getBuilder(mContext.getString(R.string.live_room_chat_rank_anchor_first)).setForegroundColor(getColor(R.color.text_chat_notify))
                                 .create();
                     } else {
                         spannableStringBuilder = SpannableStringUtils.getBuilder(mContext.getString(R.string.live_room_chat_rank_anchor_1)).setForegroundColor(getColor(R.color.text_chat_notify))
                                 .append(String.valueOf(roomRankUpgradeNotify.rank)).setForegroundColor(getColor(R.color.text_chat_share))
                                 .append(mContext.getString(R.string.live_room_chat_rank_anchor_2)).setForegroundColor(getColor(R.color.text_chat_notify))
                                 .create();
                     }
                 } else if (roomRankUpgradeNotify.rankType == 2) {   //用户日榜
                     spannableStringBuilder = SpannableStringUtils.getBuilder(mContext.getString(R.string.live_room_chat_rank_day_1)).setForegroundColor(getColor(R.color.text_chat_notify))
                             .append(roomRankUpgradeNotify.userData.nick).setForegroundColor(getColor(R.color.text_chat_at))
                             .append(mContext.getString(R.string.live_room_chat_rank_day_2)).setForegroundColor(getColor(R.color.text_chat_notify))
                             .append(String.valueOf(roomRankUpgradeNotify.rank)).setForegroundColor(getColor(R.color.text_chat_share))
                             .append(mContext.getString(R.string.live_room_chat_rank_day_3)).setForegroundColor(getColor(R.color.text_chat_notify))
                             .create();
                 } else if (roomRankUpgradeNotify.rankType == 3) {   //用户月榜
                     spannableStringBuilder = SpannableStringUtils.getBuilder(mContext.getString(R.string.live_room_chat_rank_month_1)).setForegroundColor(getColor(R.color.text_chat_notify))
                             .append(roomRankUpgradeNotify.userData.nick).setForegroundColor(getColor(R.color.text_chat_at))
                             .append(mContext.getString(R.string.live_room_chat_rank_month_2)).setForegroundColor(getColor(R.color.text_chat_notify))
                             .append(String.valueOf(roomRankUpgradeNotify.rank)).setForegroundColor(getColor(R.color.text_chat_share))
                             .append(mContext.getString(R.string.live_room_chat_rank_month_3)).setForegroundColor(getColor(R.color.text_chat_notify))
                             .create();

                 }
             } else if (item instanceof RoomHouseNotify) {  //建楼卡
                 RoomHouseNotify roomHouseNotify = (RoomHouseNotify) item;
                 userInfoCard = roomHouseNotify.sender;
                 String message = String.format(mContext.getString(R.string.house_message), roomHouseNotify.getHouseName(mContext));
                 spannableStringBuilder = SpannableStringUtils.getBuilder(space)
                         .append(message).setForegroundColor(getColor(R.color.text_chat_share))
                         .create();
             } else if (item instanceof String) {
                 String content = (String) item;
                 spannableStringBuilder = SpannableStringUtils.getBuilder(content)
                         .create();
             }
         }
        ;
        if (userInfoCard != null) {
            helper.setText(R.id.item_room_chat_tv_level, String.valueOf(userInfoCard.UserLv));
            //用户等级图标
            SpannableStringUtils.Builder levelImageSpan = SpannableStringUtils.getBuilder(String.valueOf(userInfoCard.UserLv)).setImageSpan(new VerticalImageSpan(mContext, LevelHelper.getUserLevelResId(userInfoCard.UserLv)));
            //家族称号
            if (!StringUtil.isEmpty(userInfoCard.designation)) {
                levelImageSpan.append(space)
                        .append(userInfoCard.designation).setImageSpan(new BackgroundDrawableSpan(getDrawable(R.drawable.shape_corner2_ff0), userInfoCard.designation, getDimen(R.dimen.size_9dp), getColor(R.color.text_ff), getDimen(R.dimen.size_2dp)));
            }
            //用户昵称
            levelImageSpan.append(space)
                    .append(userInfoCard.nick).setForegroundColor(getNickColor(userInfoCard));
            //先显示用户等级再追加内容
            messageTextView.setText(levelImageSpan.create());
        } else {
            helper.setText(R.id.item_room_chat_tv_level, null);
            //先清空内容
            messageTextView.setText(null);
        }

        if (spannableStringBuilder != null) {
            messageTextView.append(spannableStringBuilder);
        }
    }

    private int getColor(@ColorRes int id) {
        return mContext.getResources().getColor(id);
    }

    private Drawable getDrawable(@DrawableRes int id) {
        return mContext.getResources().getDrawable(id);
    }

    private int getDimen(@DimenRes int id) {
        return mContext.getResources().getDimensionPixelSize(id);
    }

    private int getNickColor(UserInfoCard user) {
        return mContext.getResources().getColor(LevelHelper.getUserNickColor(user.UserLv));
    }
}
